<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page session="false"%>
<%@ include file="/WEB-INF/base/include.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jmx Url management</title>

<!-- ECharts单文件引入 -->
<script type="text/javascript" charset="utf-8"
	src="${JS }/jquery-1.11.3.js"></script>

<script type="text/javascript" charset="utf-8"
	src="${ ECHAR }/www2/js/echarts-all.js"></script>


<link rel="stylesheet" charset="utf-8" type="text/css"
	href="${ BOOTSTRAP }/bootstrap-3.3.5-dist/css/bootstrap.css" />

<script type="text/javascript" charset="utf-8"
	src="${ BOOTSTRAP }/bootstrap-3.3.5-dist/js/bootstrap.js"></script>

<script type="text/javascript" charset="utf-8"
	src="${JS }/loadingUtils.js"></script>

<script type="text/javascript" charset="utf-8"
	src="${JS }/performancejs_page.js"></script>

<script type="text/javascript">
	$(function() {
		$("#addJMXUrlBtn").on("click",function() {
			var jmxUrl = $("#jmxUrl").val().trim();
			var cmd = "add";
			var jmxName = $("#jmxName").val().trim();
			var alertStr = "";
			var ifAlert = false;
			if (!jmxUrl) {
				alertStr += "some errors with jmxUrl \n";
				ifAlert = true;
			}
			if (!jmxName) {
				alertStr += "some errors with jmxName \n";
				ifAlert = true;
			}

			//var patt1=new RegExp("e");
			//document.write(patt1.test("The best things in life are free")); 

			re = new RegExp("^service:jmx:rmi:///jndi/rmi://");// 以 service:jmx:rmi 打头
			var testPass = re.test(jmxUrl);
			if (!testPass) {
				alertStr += "jmxUrl should start wiht service:jmx:rmi:///jndi/rmi:// \n";
				ifAlert = true;
			}

			if (ifAlert) {
				alert("Error: \n" + alertStr);
			} else {
				$.ajax({
					type : 'get',
					url : "<c:url value='/hpe/performance/checkJmxUrl'/>",
					data : {
						"jmxUrl" : $("#jmxUrl").val()
					},
					success : function(result) {
						loading.hideLoadingIcon("#loadingIcon");
						if (result) {
							ret = eval("(" + result+ ")");
							if (ret.status == "success") {
								$.ajax({
									type : 'get',
									url : "<c:url value='/hpe/performance/addDelJmxUrl'/>",
									data : {
										"jmxUrl" : $("#jmxUrl").val(),
										"cmd" : "add",
										"jmxName" : $("#jmxName").val()
									},
									success : function(result) {
										if (result) {
											res = eval("("+ result+ ")");
											alert(res.status);
											location = location;
										}
									},
									error : function(result) {
										alert("some error occured")
									},
									beforeSend : function() {
										//test()
										loading.showLoadingIcon("#loadingIcon");
									},
									completed : function() {
										loading.hideLoadingIcon("#loadingIcon");
									}
								});
							} else {
								alert(ret.message);
							}
						}
					},
					error : function(result) {
						loading.hideLoadingIcon("#loadingIcon");
						console.log("some error occured");
					},
					beforeSend : function() {
						loading.showLoadingIcon("#loadingIcon");
					},
					completed : function() {
						loading.hideLoadingIcon("#loadingIcon");
					}
				});

			}
		});

		$(".jmxDEL").on("click",
			function() {
				var confirmDelete = confirm('Are you sure to delete the jmx? please notes that the relate datas will delete, and this opeation can not revert.')
				if (confirmDelete) {
					var jmxUrl = $(this).parent().siblings(".jmxURLtd").html();
					var jmxName = $(this).parent().siblings(".jmxNametd").html(); 
					alert(jmxUrl)
					$.ajax({
						type : 'get',
						url : "<c:url value='/hpe/performance/addDelJmxUrl'/>",
						//service:jmx:rmi:///jndi/rmi://MYDPH0075.hpswlabs.adapps.hp.com:29601/jmxrmi
						data : {
							"jmxUrl" : jmxUrl,
							"jmxName" : jmxName
						},
						success : function(result) {
							loading.hideLoadingIcon("#loadingIcon");
							if (result) {
								res = eval("(" + result+ ")");
								alert(res.status);
								location = location;
							}
						},
						error : function(result) {
							loading.hideLoadingIcon("#loadingIcon");
							alert("some error occured");
						},
						beforeSend : function() {
							loading.showLoadingIcon("#loadingIcon");
						},
						completed : function() {
							loading.hideLoadingIcon("#loadingIcon");
						}
					});
				}

			});

		$(".jmxDynamic").on("click",
			function() {
				var currJmxUrl = $(this).parent().siblings(
						".jmxURLtd").html(); //当前元素前一个兄弟节点 
				var opUrl = "<c:url value='/hpe/performance/dynamicReport'/>"
						+ "?jmxUrl=" + currJmxUrl;
				//alert(opUrl)
				window.open(opUrl);
		});

		$(".jmxReport").on("click",
			function() {
				//打开一个页面，然后页面上有选择开始和结束时间的按钮，然后点击一个按钮，显示出对应时间段内的echart，然后导出excel文件
				var currJmxUrl = $(this).parent().siblings(
						".jmxURLtd").html(); //当前元素前一个兄弟节点 
				var opUrl = "<c:url value='/hpe/performance/exportReport'/>"
						+ "?jmxUrl=" + currJmxUrl;
				//alert(opUrl);
				window.open(opUrl);
		})

	})
</script>
</head>
<body>
	<div>
		<div id="loadingIcon"
			style="position: absolute; width: 100%; height: 100%; background-color: gray; opacity: 0.5; display: none; z-index: 99">
			<img alt="loading..." title="loading..."
				src="${IMAGES }/loading1.gif"
				style="padding-left: 48%; padding-top: 22%">
		</div>
		<br /> <input type="text" id="jmxStatus" style="visibility: hidden;">
		<div id="testDIV" class="text-center">
			<center>
				<h2>jmx list</h2>
				<c:if test="${empty jmxUrls}">no info!</c:if>
				<c:if test="${!empty jmxUrls}">
					<table
						style="border: solid #add9c0; border-width: 1px 1px 1px 1px; width: 75%"
						class="table table-bordered table-hover">
						<tbody>
							<c:forEach items="${jmxUrls}" var="item" varStatus="status">
								<tr>
									<td class="jmxNametd" style="font-size: x-large"
										title="${ item.key }">${ item.key }</td>
									<td class="jmxURLtd" title="${ item.value }">${ item.value }</td>
									<td>
										<button type="button" title="delete"
											class="jmxDEL btn btn-danger">DELETE</button>
									</td>
									<td><button title="dynamic report"
											class="jmxDynamic btn btn-primary">DYNAMIC REPORT</button></td>
									<td><button class="jmxReport btn btn-info"
											title="expport report">EXPORT REPORT</button></td>
								</tr>
							</c:forEach>
						</tbody>
					</table>
				</c:if>
			</center>
		</div>
		<hr>
		<div style="width: 75%; margin: 0 auto;">
			<h1>Create a monitor task:</h1>
			<div>
				name:<input type="text" id="jmxName" name="jmxName"
					title="input the name"></input><br /> jmxUrl:
				<textarea id="jmxUrl" name="jmxUrl" rows="3" cols="80"
					title="input the jmxurl, it is start with 'service:jmx:rmi'">service:jmx:rmi:///jndi/rmi://16.60.152.204:29602/jmxrmi</textarea>
				<br />

				<button type="button" class="btn btn-success icon-plus"
					id="addJMXUrlBtn">add</button>
			</div>
		</div>
	</div>
</body>
</html>